﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight;
using EnsureWeb.Web.DomainService;
using EnsureWeb.UI.Core;
using EnsureWeb.Web.DataModel;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using EnsureWeb.UI.Views;
using System.Windows.Controls;
using System.ServiceModel.DomainServices.Client;

namespace EnsureWeb.UI.ViewModels
{
    [StaticResource("MembershipRoleViewModel")]
    public partial class MembershipRoleViewModel : EditableViewModel
    {

        protected MembershipRole currentRole;
        public virtual MembershipRole CurrentRole
        {
            get
            {
                return currentRole;
            }
            set
            {
                currentRole = value;
                RaisePropertyChanged("CurrentRole");
            }
        }

        public override ICommand ItemEdit
        {
            get
            {
               if (itemEdit == null)
                    itemEdit = new RelayCommand(() =>
                    {
                        this.IsShowDataFormPanel = true;
                        // ...
                      
                    });

                return itemEdit;
            }
        }

        

        public override ICommand ItemAdd
        {
            get
            {
                if (itemAdd == null)
                    itemAdd = new RelayCommand<DataForm>(dataForm =>
                    {
                        this.IsShowDataFormPanel = true;
                        dataForm.AddNewItem();

                        // Add empty role-power
                        foreach (var power in this.DomainContext.MembershipPowers)
                        {
                            this.CurrentRole.MembershipRolePower.Add(
                                new MembershipRolePower()
                                    {
                                        RoleId = this.currentRole.Id,
                                        PowerId = power.Id,
                                        AddFlag = false,
                                        EditFlag = false,
                                        DelFlag = false,
                                        ExecFlag = false,
                                        GetFlag = false
                                    }
                                );
                        }
                        
                    });

                return itemAdd;
            }
        }
        
        public override void Init()
        {          

        }
    }

}